﻿namespace CSharpPro._1028;

public class UserService : IUserService
{
    // 泛型集合存储用户对象
    private List<User> _userList = new List<User>();

    public void ShowMainMenu()
    {
        while (true)
        {
            Console.WriteLine("主菜单：1. 登录 2. 注册 3. 退出");
            Console.Write("请输入数字选择功能：");
            string choice = Console.ReadLine().Trim();

            switch (choice)
            {
                case "1":
                    Login();
                    break;
                case "2":
                    Regist();
                    break;
                case "3":
                    Console.WriteLine("退出程序");
                    return;
                default:
                    Console.WriteLine("请输入有效数字");
                    break;
            }
        }
    }

    public void Login()
    {
        Console.Write("请输入用户名：");
        string username = Console.ReadLine().Trim();
        if (string.IsNullOrWhiteSpace(username))
        {
            Console.WriteLine("用户名不可为空");
            return;
        }

        Console.Write("请输入密码：");
        string password = Console.ReadLine().Trim();
        if (string.IsNullOrWhiteSpace(password))
        {
            Console.WriteLine("密码不可为空");
            return;
        }

        // 遍历泛型集合（foreach循环迭代）
        User? targetUser = null;
        foreach (User user in _userList)
        {
            if (user.Username == username)
            {
                targetUser = user;
                break;
            }
        }

        if (targetUser == null)
        {
            Console.WriteLine("用户名不存在");
        }
        else if (targetUser.Password != password)
        {
            Console.WriteLine("密码错误");
        }
        else
        {
            Console.WriteLine("登录成功，直接进入系统首页（模拟）");
        }
    }

    public void Regist()
    {
        Console.Write("请输入用户名：");
        string username = Console.ReadLine().Trim();

        // 用户名校验
        if (string.IsNullOrWhiteSpace(username))
        {
            Console.WriteLine("用户名不可为空");
            return;
        }
        if (username.Length < 3 || username.Length > 15)
        {
            Console.WriteLine("用户名长度必须为3-15个字符");
            return;
        }
        foreach (char c in username)
        {
            if (!char.IsLetterOrDigit(c) && c != '_')
            {
                Console.WriteLine("用户名只能包含字母、数字、下划线");
                return;
            }
        }
        // 检查用户名是否已存在（foreach循环迭代）
        foreach (User user in _userList)
        {
            if (user.Username == username)
            {
                Console.WriteLine("用户名已存在");
                return;
            }
        }

        Console.Write("请输入密码：");
        string password = Console.ReadLine().Trim();
        Console.Write("请确认密码：");
        string confirmPwd = Console.ReadLine().Trim();

        // 密码校验
        if (string.IsNullOrWhiteSpace(password))
        {
            Console.WriteLine("密码不可为空");
            return;
        }
        if (password.Length < 6 || password.Length > 20)
        {
            Console.WriteLine("密码长度必须为6-20个字符");
            return;
        }
        bool hasUpper = false, hasLower = false, hasDigit = false;
        foreach (char c in password)
        {
            if (char.IsUpper(c)) hasUpper = true;
            else if (char.IsLower(c)) hasLower = true;
            else if (char.IsDigit(c)) hasDigit = true;
        }
        if (!hasUpper || !hasLower || !hasDigit)
        {
            Console.WriteLine("密码必须包含至少1个大写字母、1个小写字母和1个数字");
            return;
        }
        if (password != confirmPwd)
        {
            Console.WriteLine("密码与确认密码必须一致");
            return;
        }

        // 添加新用户到泛型集合
        _userList.Add(new User(username, password));
        Console.WriteLine("注册成功");
    }
}